﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
namespace WebApplication.Admin
{
    public partial class PhanQuyen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
                Loadnhanvien();
            }

        }
        public void LoadHienthi()
        {
            string strid=""+Request["MANV"];
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            NHANVIEN objNhanvien=null;
            if(!string.IsNullOrEmpty(strid))
            {
                objNhanvien=repository.GetById(strid);
                Dlnhanvien.SelectedValue = objNhanvien.MANV; 
            }
            QUYENRepository repositoryquyen = new QUYENRepository(new EFRepository<QUYEN>(), iunitfwork);
            CHUCNANGRepository repositorychucnang = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            NGUOUDUNG_CHUCNANGRepository repositorynguoidung_cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            List<int> obJngd_cn = repositorynguoidung_cn.All().Where(p => p.MANV == strid).Select(t => (int)t.ID_CHUCNANG).ToList();
            IQueryable<CHUCNANG> Lstchucnang = repositorychucnang.All();
            List<int> LstIdquyen = new List<int>();
            foreach (int i in obJngd_cn)
            {
                foreach (CHUCNANG cn in Lstchucnang)
                {
                    if (i == cn.ID)
                    {
                        LstIdquyen.Add((int)cn.ID_QUYEN);
                    }
                }
            }
            List<QUYEN> Lstchucnangdagan = repositoryquyen.All().Where(p => LstIdquyen.Contains((int)p.ID)).ToList();
            ///Quyền đã gán
            lbquyendacap.DataSource = Lstchucnangdagan;
            lbquyendacap.DataValueField = "ID";
            lbquyendacap.DataTextField = "TENQUYEN";
            lbquyendacap.DataBind();
            ///Quyền chưa gán
            ///

            var LstChucnangchuagan = repositoryquyen.All().Where(p => !LstIdquyen.Contains((int)p.ID)).ToList();
            Lbquyenchuacap.DataSource = LstChucnangchuagan;
            Lbquyenchuacap.DataValueField = "ID";
            Lbquyenchuacap.DataTextField = "TENQUYEN";
            Lbquyenchuacap.DataBind();

        }
        public void Loadnhanvien()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            IQueryable<NHANVIEN> objNhanvien = repository.All();
            Dlnhanvien.DataSource = objNhanvien;
            Dlnhanvien.DataTextField = "TENNV";
            Dlnhanvien.DataValueField = "MANV";
            Dlnhanvien.DataBind();
            Dlnhanvien.Items.Insert(0, new ListItem { Text = "--Chọn nhân viên---", Value = "0" });
        }
        protected void lbquyendacap_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = 0;
            string strid = lbquyendacap.SelectedValue;
            int.TryParse(strid, out id);
            IUnitOfWork iunitfwork=new EFUnitOfWork();
            QUYENRepository repositoryquyen = new QUYENRepository(new EFRepository<QUYEN>(), iunitfwork);
            QUYEN objQUyen = repositoryquyen.GetById(id);
            CHUCNANGRepository repository = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            NGUOUDUNG_CHUCNANGRepository repositoryND_Cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            IQueryable<NGUOUDUNG_CHUCNANG> objNd_Cn = repositoryND_Cn.All().Where(p => p.MANV == Dlnhanvien.SelectedValue);
            CHUCNANG objchucnang = null;
            foreach (NGUOUDUNG_CHUCNANG cd_Cn in objNd_Cn)
            {
                objchucnang = dataprovider.Entity.CHUCNANGs.Where(p => p.ID_QUYEN == objQUyen.ID && p.ID == cd_Cn.ID_CHUCNANG).FirstOrDefault();
                 if (objchucnang != null)
                 {
                     break;
                 }
            }

            if (objchucnang != null)
            {
                cksua.Checked = false;
                ckthemmoi.Checked = false;
                ckxoa.Checked = false;
               
                if (objchucnang.ISADD==true)
                {
                    ckthemmoi.Checked = true;
                }
                if (objchucnang.ISDELETE==true)
                {
                    ckxoa.Checked = true;
                }
                if (objchucnang.ISEDIT==true)
                {
                    cksua.Checked = true;
                }
                
            }
        }
        /// <summary>
        /// Chọn quyền
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnchon_Click(object sender, EventArgs e)
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHUCNANGRepository repository = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            string strid = Lbquyenchuacap.SelectedValue;
            int id=0;
            int.TryParse(strid,out id);
            NGUOUDUNG_CHUCNANGRepository repositoryNd_Cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            CHUCNANGRepository repositoryCn=new CHUCNANGRepository(new EFRepository<CHUCNANG>(),iunitfwork);
            IQueryable<CHUCNANG> LSTCn=repositoryCn.All().Where(p=>p.ID_QUYEN==id);
            NGUOUDUNG_CHUCNANG objNguoiDung_Cn = new NGUOUDUNG_CHUCNANG();
            IQueryable<NGUOUDUNG_CHUCNANG> LstNguoiDungChucnang = repositoryNd_Cn.All();
            List<string> LStstring = new List<string>();
            //foreach (NGUOUDUNG_CHUCNANG Nd_Cn in LstNguoiDungChucnang)
            //{
            //    if (Dlnhanvien.SelectedValue.Trim() == Nd_Cn.MANV)
            //    {
            //        foreach(CHUCNANG cn in LSTCn)
            //        {
            //            if (Nd_Cn.ID_CHUCNANG == cn.ID)
            //            {
            //                RegisterClientScriptBlock("Thông báo", "<Script> alert('Đã tồn tại')");
            //                LStstring.Add("ds");
            //                return;
            //            }
            //        }
            //        if (LStstring.Count > 0)
            //        {
            //            return;
            //        }
                    
            //    }
            //}
            CHUCNANG objChucnang = new CHUCNANG();
            if (id > 0)
            {
                objChucnang.ISLIST = true;
                objChucnang.ID_QUYEN = int.Parse(Lbquyenchuacap.SelectedValue);
                repository.Add(objChucnang);
                repository.Save();
                hfId.Value = objChucnang.ID.ToString();
                objNguoiDung_Cn.MANV = Dlnhanvien.SelectedValue;
                objNguoiDung_Cn.ID_CHUCNANG = int.Parse(hfId.Value);
                repositoryNd_Cn.Add(objNguoiDung_Cn);
                repositoryNd_Cn.Save();
            }
            dlnhanvien();

        }
        /// <summary>
        /// Hủy quyền
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnbochon_Click(object sender, EventArgs e)
        {
            dlnhanvien();
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHUCNANGRepository repository = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            string strid = lbquyendacap.SelectedValue;
            int id = 0;
            int.TryParse(strid, out id);
            NGUOUDUNG_CHUCNANGRepository repositoryNd_Cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            NGUOUDUNG_CHUCNANG objnd_cn1 = repositoryNd_Cn.All().Where(p => p.MANV == Dlnhanvien.SelectedValue).FirstOrDefault();
            NGUOUDUNG_CHUCNANG objNguoiDung_Cn =null;
            CHUCNANG objChucnangs = null;
            int idcn = 0;
            if (id > 0)
            {
                CHUCNANG objChucnang = null;
                List<CHUCNANG> Lstchucnang = dataprovider.Entity.CHUCNANGs.Where(p => p.ID_QUYEN == id).ToList();
             foreach (CHUCNANG cn in Lstchucnang)
             {
                 if (cn.ID == objnd_cn1.ID_CHUCNANG)
                 {
                     idcn = (int)objnd_cn1.ID_CHUCNANG;
                     objChucnang = dataprovider.Entity.CHUCNANGs.Where(p => p.ID == idcn).FirstOrDefault();
                     break;
                 }
             }
             if (objChucnang != null)
             {
                 objNguoiDung_Cn = dataprovider.Entity.NGUOUDUNG_CHUCNANG.Where(p => p.ID_CHUCNANG == objChucnang.ID && p.MANV == Dlnhanvien.SelectedValue).FirstOrDefault();
                 dataprovider.Entity.DeleteObject(objNguoiDung_Cn);
                 dataprovider.Entity.SaveChanges();
                 objChucnangs = dataprovider.Entity.CHUCNANGs.Where(p => p.ID_QUYEN == id && p.ID == objNguoiDung_Cn.ID_CHUCNANG).FirstOrDefault();
                 dataprovider.Entity.CHUCNANGs.DeleteObject(objChucnang);
                 dataprovider.Entity.SaveChanges();
             }
            }
            dlnhanvien();
            

        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            bool iscapnha = true;
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NGUOUDUNG_CHUCNANGRepository repositoryNd_Cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            CHUCNANGRepository repository = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            string strid = lbquyendacap.SelectedValue;
            int id = 0;
            int.TryParse(strid, out id);
            CHUCNANG Objchucnang = null;
            NGUOUDUNG_CHUCNANG objNd_Cn = null;
            string MANV = "";
            if (Dlnhanvien.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng chọn nhân viên')</script>");
                return;
            }
            if (id > 0)
            {
                IQueryable<NGUOUDUNG_CHUCNANG> LstNd_Cn = repositoryNd_Cn.All();
                IQueryable<CHUCNANG> Lstchucnang = repository.All().Where(p => p.ID_QUYEN == id);
                int idcn = 0;
                foreach (NGUOUDUNG_CHUCNANG nd_cn in LstNd_Cn)
                {
                    foreach (CHUCNANG cn in Lstchucnang)
                    {
                        if (cn.ID == nd_cn.ID_CHUCNANG && nd_cn.MANV==Dlnhanvien.SelectedValue)
                        {
                            MANV = nd_cn.MANV;
                            idcn = (int)nd_cn.ID_CHUCNANG;
                            break;
                        }
                        if (!string.IsNullOrEmpty(MANV))
                        {
                            break;
                        }
                    }
                }
                if (MANV == Dlnhanvien.SelectedValue)
                {
                    objNd_Cn = dataprovider.Entity.NGUOUDUNG_CHUCNANG.Where(p => p.MANV == MANV && p.ID_CHUCNANG == idcn).FirstOrDefault();
                }
                if (objNd_Cn != null)
                {
                    Objchucnang = dataprovider.Entity.CHUCNANGs.Where(p => p.ID == objNd_Cn.ID_CHUCNANG).FirstOrDefault();
                    iscapnha = false;
                }
                    Objchucnang.ISEDIT =cksua.Checked;

                    Objchucnang.ISADD = ckthemmoi.Checked;
                

                    Objchucnang.ISDELETE =ckxoa.Checked;
               
                dataprovider.Entity.SaveChanges();

            }
           
        }

        protected void Dlnhanvien_SelectedIndexChanged(object sender, EventArgs e)
        {

            dlnhanvien();
        }
        void dlnhanvien()
        {
            string strid = Dlnhanvien.SelectedValue;
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            NHANVIEN objNhanvien = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objNhanvien = repository.GetById(strid);
                Dlnhanvien.SelectedValue = objNhanvien.MANV;
            }
            QUYENRepository repositoryquyen = new QUYENRepository(new EFRepository<QUYEN>(), iunitfwork);
            CHUCNANGRepository repositorychucnang = new CHUCNANGRepository(new EFRepository<CHUCNANG>(), iunitfwork);
            NGUOUDUNG_CHUCNANGRepository repositorynguoidung_cn = new NGUOUDUNG_CHUCNANGRepository(new EFRepository<NGUOUDUNG_CHUCNANG>(), iunitfwork);
            List<int> obJngd_cn = repositorynguoidung_cn.All().Where(p => p.MANV == strid).Select(t => (int)t.ID_CHUCNANG).ToList();
            IQueryable<CHUCNANG> Lstchucnang = repositorychucnang.All();
            List<int> LstIdquyen = new List<int>();
            foreach (int i in obJngd_cn)
            {
                foreach (CHUCNANG cn in Lstchucnang)
                {
                    if (i == cn.ID)
                    {
                        LstIdquyen.Add((int)cn.ID_QUYEN);
                    }
                }
            }
            List<QUYEN> Lstchucnangdagan = repositoryquyen.All().Where(p => LstIdquyen.Contains((int)p.ID)).ToList();
            ///Quyền đã gán
            lbquyendacap.DataSource = Lstchucnangdagan;
            lbquyendacap.DataValueField = "ID";
            lbquyendacap.DataTextField = "TENQUYEN";
            lbquyendacap.DataBind();
            ///Quyền chưa gán
            ///

            var LstChucnangchuagan = repositoryquyen.All().Where(p => !LstIdquyen.Contains((int)p.ID)).ToList();
            Lbquyenchuacap.DataSource = LstChucnangchuagan;
            Lbquyenchuacap.DataValueField = "ID";
            Lbquyenchuacap.DataTextField = "TENQUYEN";
            Lbquyenchuacap.DataBind();
        }
    }
}